/**
 * @Description:
 * @author 若琳
 * @date 2021/7/16
 */

const express = require('express');
const RoleController = express.Router();
//数据集合
const ResultJson = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/ResultJson.js").V("v1.0.0");
const LayuiGrid = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/layuiGrid.js");
const RoleService = require("../../../../../../../../ruoling-system/src/com/ruoling/system/service/v1/system/service.role.js");

/**
 * 角色列表视图
 */
RoleController.get("/system/role/list/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysrole/roleview.html");
});

/**
 * 角色添加视图
 */
RoleController.get("/system/role/add/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysrole/roleaddview.html");
});

/**
 * 角色编辑视图
 */
RoleController.get("/system/role/edit/view",function (Request,response){

    let Id = Request.query.Id;
    RoleService.RoleByIdEditDataService(Id,(err,data)=>{
        response.type(".html");
        response.render("admin/system/sysrole/roleeditview.html",{data:data});
    })

});


/**
 * 角色复选框树结构
 */
RoleController.get("/v1/api/role/menuchecktree",function (Request,response){

    try{
        RoleService.RoleMenuCheckTreeService((err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(new LayuiGrid(0,data));
            return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 角色添加
 */
RoleController.post("/v1/api/role/add",function (Request,response){

    try{
        let {sys_role_name,sys_role_sort,sys_role_state,arr_Id} = Request.body;

        RoleService.RoleAddService(sys_role_name,sys_role_sort,sys_role_state,arr_Id,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
                response.send(ResultJson.MegOk("角色添加成功"));
                return;
        })
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 角色列表
 */
RoleController.get("/v1/api/role/list",function (Request,response){

    try{
        let {limit,page} = Request.query;

        RoleService.RoleListService(limit,page,(err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(new LayuiGrid(data.pager.totalRecord,data.pager.dataList));
            return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 角色编辑
 */
RoleController.post("/v1/api/role/edit",function (Request,response){

    try{
        let {sys_role_name,sys_role_sort,sys_role_state,arr_Id,ID} = Request.body;
        RoleService.RoleEditServie(sys_role_name,sys_role_sort,sys_role_state,arr_Id,ID,(err,data)=>{
            if(err){

                response.send(ResultJson.GUBError());
                return;
            }

                response.send(ResultJson.MegOk("角色修改成功"));
                return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 角色
 */
RoleController.post("/v1/api/role/rolecheckarr",function (Request,response){

    try{

        RoleService.RoleCheckarrServie((err,data)=>{
            if(err){

                response.send(ResultJson.GUBError());
                return;
            }
                response.send(ResultJson.Ok(data));
                return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});




module.exports = RoleController;
